package com.kacheap.configuration;

import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

import com.kacheap.data.enums.OrderStatus;
import com.kacheap.data.enums.OrderStatusChangeEvent;
import com.kacheap.data.pojo.Order;

@Component
@WithStateMachine(name = "orderStateMachine")
public class OrderStateListener {

	@OnTransition(source = "WAIT_PAYMENT", target = "WAIT_DELIVER")
	public boolean payTransition(Message<OrderStatusChangeEvent> message) {
		Order order = message.getHeaders().get("order", Order.class);
		
		order.setStatus(OrderStatus.WAIT_DELIVER);
		
		System.out.println("支付，状态机反馈信息：" + message.getHeaders().toString());
		
		return true;
	}
	

	@OnTransition(source = "WAIT_DELIVER", target = "WAIT_RECEIVE")
	public boolean deliverTransition(Message<OrderStatusChangeEvent> message) {
		Order order = message.getHeaders().get("order", Order.class);
		
		order.setStatus(OrderStatus.WAIT_RECEIVE);
		
		System.out.println("发货，状态机反馈信息：" + message.getHeaders().toString());
		
		return true;
	}
	

	@OnTransition(source = "WAIT_RECEIVE", target = "FINISH")
	public boolean receiveTransition(Message<OrderStatusChangeEvent> message) {
		Order order = message.getHeaders().get("order", Order.class);
		
		order.setStatus(OrderStatus.FINISH);
		
		System.out.println("收货，状态机反馈信息：" + message.getHeaders().toString());
		
		return true;
	}
	
	
	@OnTransition(source = "FINISH", target = "WAIT_DELIVER")
	public boolean refundTransition(Message<OrderStatusChangeEvent> message) {
		Order order = message.getHeaders().get("order", Order.class);
		
		order.setStatus(OrderStatus.WAIT_DELIVER);
		
		System.out.println("收货，状态机反馈信息：" + message.getHeaders().toString());
		
		return true;
	}
	
}